const router = require ("koa-router")();
const MembersModel = require("../../models/Members");
const upload = require("../../config/multer")
router.post("/m1/doAdd",upload.single('file'),async ctx=>{
    // 这里数据要用ctx.req.body来获取
    // 保存文件到数据库
    let {id,name,age,like,friend,state} = ctx.req.body;
    friend = friend?true:false;
    state = state?true:false;
    var imageUrl =`http://${ctx.request.header.host}/${ctx.req.file.filename}`;
    if(id){
        await MembersModel.findByIdAndUpdate(id,{
            name,age,like,friend,state,imageUrl
        })
    }else{
        var data = new MembersModel({name,age,like,friend,state,imageUrl});
        data.save(err=>{
            if(err){throw err};
        });
    }

    ctx.redirect("/");
})
module.exports = router;